package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import static thread.MyThreadPools.count;

/**
 * @Author liangzai
 * @Description:
 */

class MyThreadPools{
    public static int count  = 0;
    private BlockingQueue<Integer> queue = null;
    private Object locker = new Object();

    public MyThreadPools(int n){
        queue = new ArrayBlockingQueue<>(1000);
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(()->{
                while (true) {
                        try {
                            int key = queue.take();
                            System.out.println("消费元素：" + key);
                            synchronized (locker) {
                            count++; }
                            System.out.println("count="+count);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                }
            });
            thread.start();
        }
    }

    public void submit(int n) throws InterruptedException {
            queue.put(n);
            System.out.println("生产元素："+n);
    }

}

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        int c = count;
        MyThreadPools threadPools = new MyThreadPools(4);
        for (int i = 0; i <= 1000; i++) {
            threadPools.submit(i);
        }
        //System.out.println(c);
    }
}
